<?php
/**
 * 合同管理
 * Created by PhpStorm.
 * User: g
 * Date: 2018/1/26
 * Time: 11:04
 */

namespace app\index\controller;


use app\index\validate\Valid;
use think\Request;

class Htgl extends Common
{
    //我的合同申请
    public function wdhtsq(){
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        $htzt = $this->request->get('htzt');
        $this->assign('htzt',$htzt);
        $htlx = $this->request->get('htlx');
        $this->assign('htlx',$htlx);
        $htje_min = $this->request->get('htje_min');
        $this->assign('htje_min',$htje_min);
        $htje_max = $this->request->get('htje_max');
        $this->assign('htje_max',$htje_max);
        $htqsf_wfmc = $this->request->get('htqsf_wfmc');
        $this->assign('htqsf_wfmc',$htqsf_wfmc);
        $htqsf_dfmc = $this->request->get('htqsf_dfmc');
        $this->assign('htqsf_dfmc',$htqsf_dfmc);
        $htqsrq_min = $this->request->get('htqsrq_min');
        $this->assign('htqsrq_min',$htqsrq_min);
        $htqsrq_max = $this->request->get('htqsrq_max');
        $this->assign('htqsrq_max',$htqsrq_max);
        $sqr = $this->request->get('sqr');
        $this->assign('sqr',$sqr);
        $status = $this->request->get('status');
        $this->assign('status',$status);

        $where = [];
        if($sn){
            $where['sn'] = ['like',"%{$sn}%"];
        }
        if($htzt){
            $where['htzt'] = ['like',"%{$htzt}%"];
        }
        if($htlx){
            $where['htlx'] = ['like',"%{$htlx}%"];
        }
        if($htje_min&&$htje_max){
            $where['htje'] = [['>',$htje_min],['<',$htje_max],'and'];
        }elseif($htje_min){
            $where['htje'] = ['>',$htje_max];
        }elseif($htje_max){
            $where['htje'] = ['<',$htje_max];
        }

        if($htqsf_wfmc){
            $where['htqfs_wfmc'] = ['like',"%{$htqsf_wfmc}%"];
        }
        if($htqsf_dfmc){
            $where['htqfs_dfmc'] = ['like',"%{$htqsf_dfmc}%"];
        }

        if($htqsrq_min&&$htqsrq_max){
            $where['htqdrq'] = [['>',$htqsrq_min],['<',$htqsrq_max],'and'];
        }elseif($htqsrq_min){
            $where['htqdrq'] = ['>',$htqsrq_max];
        }elseif($htqsrq_max){
            $where['htqdrq'] = ['<',$htqsrq_max];
        }
        if($sqr){
            $where['sqr_name'] = ['like',"%{$sqr}%"];
        }

        $count = db('cg_contract')->where($where)->count();
        $data = db('cg_contract')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
     	$maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k] = $v;
        }
        //查询合同类型
        $htlx = db('cg_contract_htlx')->select();
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'maxpage'=>$maxpage,
            'count'=>$count,
            'htlx'=>$htlx
        ]);
        return $this->fetch();
    }

    //所有合同管理
    public function syhtgl(){
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        $htzt = $this->request->get('htzt');
        $this->assign('htzt',$htzt);
        $htlx = $this->request->get('htlx');
        $this->assign('htlx',$htlx);
        $htje_min = $this->request->get('htje_min');
        $this->assign('htje_min',$htje_min);
        $htje_max = $this->request->get('htje_max');
        $this->assign('htje_max',$htje_max);
        $htqsf_wfmc = $this->request->get('htqsf_wfmc');
        $this->assign('htqsf_wfmc',$htqsf_wfmc);
        $htqsf_dfmc = $this->request->get('htqsf_dfmc');
        $this->assign('htqsf_dfmc',$htqsf_dfmc);
        $htqsrq_min = $this->request->get('htqsrq_min');
        $this->assign('htqsrq_min',$htqsrq_min);
        $htqsrq_max = $this->request->get('htqsrq_max');
        $this->assign('htqsrq_max',$htqsrq_max);
        $sqr = $this->request->get('sqr');
        $this->assign('sqr',$sqr);
        $status = $this->request->get('status');
        $this->assign('status',$status);
        $htjd = $this->request->get('htjd');
        $this->assign('htjd',$htjd);
        $fkjd = $this->request->get('fkjd');
        $this->assign('fkjd',$fkjd);

        $where = [];
        if($sn){
            $where['sn'] = ['like',"%{$sn}%"];
        }
        if($htzt){
            $where['htzt'] = ['like',"%{$htzt}%"];
        }
        if($htlx){
            $where['htlx'] = ['like',"%{$htlx}%"];
        }
        if($htje_min&&$htje_max){
            $where['htje'] = [['>',$htje_min],['<',$htje_max],'and'];
        }elseif($htje_min){
            $where['htje'] = ['>',$htje_max];
        }elseif($htje_max){
            $where['htje'] = ['<',$htje_max];
        }

        if($htqsf_wfmc){
            $where['htqfs_wfmc'] = ['like',"%{$htqsf_wfmc}%"];
        }
        if($htqsf_dfmc){
            $where['htqfs_dfmc'] = ['like',"%{$htqsf_dfmc}%"];
        }

        if($htqsrq_min&&$htqsrq_max){
            $where['htqdrq'] = [['>',$htqsrq_min],['<',$htqsrq_max],'and'];
        }elseif($htqsrq_min){
            $where['htqdrq'] = ['>',$htqsrq_max];
        }elseif($htqsrq_max){
            $where['htqdrq'] = ['<',$htqsrq_max];
        }
        if($sqr){
            $where['sqr_name'] = ['like',"%{$sqr}%"];
        }
        if($htjd){
            $where['htjd'] = $htjd;
        }
        if($fkjd){
            $where['fkjd'] = $fkjd;
        }

        $count = db('cg_contract')->where($where)->count();
        $data = db('cg_contract')->where($where)->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $v['htlx_name'] = db('cg_contract_htlx')->where('id',$v['htlx'])->value('title');
            $data[$k] = $v;
        }
        //查询合同类型
        $htlx = db('cg_contract_htlx')->select();
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'maxpage'=>$maxpage,
            'count'=>$count,
            'htlx'=>$htlx
        ]);
        return $this->fetch();
    }
    //新增合同
    public function xzht(){
        if($_POST){
            $data = Request::instance()->post();
            $valid = new Valid([
               'htzt|合同主题'=>'require',
               'htlx|合同类型'=>'require',
               'htqsf_wfmc|合同签署方-我方名称'=>'require',
               'htqsf_dfmc|合同签署方-对方名称'=>'require',
               'htje|合同金额'=>'require',
               'htqdrq|合同签订日期'=>'require|dateformat:Y-m-d',
               'htyxq_ksrq|合同有效期-开始日期'=>'require|dateformat:Y-m-d',
               'htyxq_jsrq|合同有效期-结束日期'=>'require|dateformat:Y-m-d',
               'htnrzy|合同内容摘要'=>'require',
               'htxq|合同详情'=>'require'
            ]);
            if(!$valid->check($data)){
                echo json_encode(['success'=>false,'msg'=>$valid->getError()]);die;
            }
            $data['ctime'] = date('Y-m-d H:i:s');
            $data['sqr'] = session('employee_id');
            $data['sqr_name'] = db('employee')->where('id',session('employee_id'))->value('em_name');
            $data['status'] = 1;
            $data['sn'] = $this->produceSN();
            $bool = db('cg_contract')->insertGetId($data);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'添加成功','id'=>$bool]);
            }else{
                echo json_encode(['success'=>false,'msg'=>'添加失败']);
            }
            die;
        }
        return $this->fetch();
    }

    //提交工作流
    public function tjgzl(){
        $id = $this->request->param('id');
        $flowId = db('cg_workflowset')->where(['id'=>'3'])->value('flow_id');
        $data = [];
        $data['accessToken'] = session('accessToken');
        $data['flowId'] = $flowId;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/open.wf',$data);
        $insert = [];
        $insert['ywlx'] = 3;
        $row = db('cg_ti')->where('id',$id)->field('title,sn')->find();
        $insert['rwmcjbh'] = $row['title'].'['.$row['sn'].']';
        $insert['yw_id'] = $id;
        $insert['status'] = 1;
        $insert['department_id'] = db('department_employee')->where(['index'=>1,'employee_id'=>session('employee_id')])->value('department_id');
        $insert['sqr_id'] = session('employee_id');
        $insert['workId'] = $data->workId;
        $insert['trackId'] = $data->flowNode->trackId;
        db('cg_workflow')->insert($insert);
        $bool = db('cg_contract')->where('id',$id)->update(['status'=>3]);
        if(false!==$bool){
            echo json_encode(['success'=>true,'msg'=>'提交成功']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'提交失败']);
        }
    }

    //生成编号
    public function produceSN(){
        //生成编号
        $max = db('cg_contract')->where('sn','like',date('Ymd').'%')->max('sn'); //编号递增开始位置基础

        if($max){
            $max = (double)$max;
            $insert_sn = $max+1;
        }else{
            $insert_sn = date('Ymd').'0001';
        }
        return $insert_sn;
    }

    //管理合同
    public function glht(){
        $id = $this->request->param('id');
        if($_POST){
            $data = Request::instance()->post();
            $valid = new Valid([
                'htjd|合同进度'=>'require',
                'fkjd|付款进度'=>'require',
                'fkje|付款金额'=>'require',
                'fkrq|付款日期'=>'require|dateformat:Y-m-d',
                'fkpz|付款凭证'=>'require',
            ]);
            if(!$valid->check($data)){
                echo json_encode(['success'=>false,'msg'=>$valid->getError()]);die;
            }
            $data['fkczsj'] = date('Y-m-d H:i:s');
            $data['fkr'] = session('employee_id');
            $data['fkr_name'] = db('employee')->where('id',session('employee_id'))->value('em_name');
            $bool = db('cg_contract')->where('id',$id)->update($data);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'付款成功']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'付款失败']);
            }
            die;
        }
        $data = db('cg_contract')->where('id',$id)->find();
        $data['htlx_name'] = db('cg_contract_htlx')->where('id',$data['htlx'])->value('title');
        $this->assign('data',$data);
        return $this->fetch();
    }
}